package _2Session;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SetSessionServlet
 */
@WebServlet("/setsession")
public class SetSessionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SetSessionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		// 1. 得到会话对象
				// 用户第一次访问 会创建Session,如果不是第一次，则得到它当前的会话
				HttpSession session = request.getSession();
				
				
				// 2. 存入信息
				session.setAttribute("name", "tom");
				session.setAttribute("age", "18");
				
				// 3. 设定有效时间(单位是秒，默认30分钟）
				session.setMaxInactiveInterval(3*60); // 从最后一次访问连续3分钟没有再次访问网站session 就会失效
//				session.setMaxInactiveInterval(30*60); 默认值
				
				// 4. 让会话失效(用户登出)
				session.invalidate();
				
				
				
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().print("设置会话信息");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
